/* global Cesium viewer */
/**
 * 坐标飞到指定视野
 * @param {} positions
 * @param {*} scale
 * @param {*} callBackFun
 * @returns
 */
function flyToBoundingSphere(positions, scale = 10, callBackFun, duration = 0.8) {
    if (!positions || positions.length < 1) return;
    let boundingSphere = Cesium.BoundingSphere.fromPoints(positions);
    viewer.camera.flyToBoundingSphere(boundingSphere, {
        offset: new Cesium.HeadingPitchRange(0, -Math.PI / 2, boundingSphere.radius * scale),
        complete: callBackFun,
        duration: duration,
    });
}
/**
 * 世界坐标转经纬度坐标
 */
function coordinateTra(points) {
    if (Array.isArray(points)) {
        let latLngArr = [];
        points.forEach(item => {
            let ellipsoid = viewer.scene.globe.ellipsoid;
            let cartographic = ellipsoid.cartesianToCartographic(item);
            let lat = Cesium.Math.toDegrees(cartographic.latitude);
            let lng = Cesium.Math.toDegrees(cartographic.longitude);
            let alt = cartographic.height;
            latLngArr.push({
                lng,
                lat,
                alt,
            });
        });
        return latLngArr;
    }
    console.error('非数组不配合');
}
/**
 * 经纬度坐标转世界坐标
 * @param {*} arr
 * @returns
 */
function latLngToCoordinate(points) {
    if (Array.isArray(points)) {
        let coordinates = [];
        points.forEach(item => {
            let eleCoordinate = Cesium.Cartesian3.fromDegrees(item[0], item[1], 0);
            coordinates.push(eleCoordinate);
        });
        return coordinates;
    }
    console.error('非数组不配合');
}

/**
 * 方形区域拼接wkt
 */
function joinWkt(arr) {
    let wktStr = '';
    if (arr?.length > 0) {
        wktStr = 'POLYGON((';
        arr.forEach((item, index) => {
            if (index === 0) {
                wktStr = wktStr + item.lng + ' ' + item.lat;
            } else {
                wktStr = wktStr + ',' + item.lng + ' ' + item.lat;
            }
        });
        wktStr += '))';
    }

    return wktStr;
}
/**
 * 世界坐标转像素坐标
 */
function coordinateToPixel(points) {
    if (Array.isArray(points)) {
        let sceneLocation = [];
        points.forEach(item => {
            let poxel = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, item);
            sceneLocation.push(poxel);
        });
        return sceneLocation;
    }
    console.error('非数组不配合');
}
/**
 * 世界坐标转像素坐标,并填充灰度值
 */
function coordinateToPixelAddGray(points, valArr) {
    if (Array.isArray(points)) {
        let sceneLocation = [];
        points.forEach((item, index) => {
            let poxel = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, item);
            poxel.x = poxel.x - 613;
            poxel.y = poxel.y - 215;
            poxel.value = valArr[index];
            sceneLocation.push(poxel);
        });
        return sceneLocation;
    }
    console.error('非数组不配合');
}

export {
    flyToBoundingSphere,
    coordinateTra,
    joinWkt,
    coordinateToPixelAddGray,
    latLngToCoordinate,
    coordinateToPixel,
};
